package com.example.consumer.topic;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author: dangwen
 * @createDate: 2020/12/6
 * 主题消息消费者
 */
@Component
public class TopicConsumers {

    /**
     * 短信消息消费者，对应的routingKey是: user.rk.sms | order.rk.sms | pay.rk.sms | reg.rk.sms
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = "queue-sms-topic"),
                    exchange = @Exchange(value = "ex-topic", type = "topic"),
                    key = "*.rk.sms"
            )
    })
    public void onUserSMSMessage(String message){
        System.out.println("短信消息内容是：" + message);
    }

    /**
     * 邮件消息消费者，对应的routingKey是: user.rk.email | order.rk.email | pay.rk.email | reg.rk.email
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = "queue-email-topic"),
                    exchange = @Exchange(value = "ex-topic", type = "topic"),
                    key = "*.rk.email"
            )
    })
    public void onUserEmailMessage(String message){
        System.out.println("邮箱消息内容是：" + message);
    }

    /**
     * 所有的和rk有关的消息，统一处理消费
     * 对应的routingKey是: user.rk.email | order.rk.email | pay.rk.email | reg.rk.email |
     *  user.rk.sms | order.rk.sms | pay.rk.sms | reg.rk.sms
     *  主要功能：不发短信，不发邮件，作为一个日记记录工具存在
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = "queue-all-topic"),
                    exchange = @Exchange(value = "ex-topic", type = "topic"),
                    key = "*.rk.*"
            )
    })
    public void onUserLogMessage(String message){
        System.out.println("执行的消息处理的逻辑是：" + message);
    }
}
